package com.smap.control;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.smap.RS;
import com.smap.core.Control;
import com.smap.core.Map;

public class ToolBar extends Composite implements ClickHandler {
	Map map;
	Panel btnGroup;
	List<PushButton> btns = new ArrayList<PushButton>();

	public ToolBar(Map map) {
		this.map = map;
		Panel panel = new VerticalPanel();
		initWidget(panel);
		this.setStyleName("ToolBar");
		btnGroup = new VerticalPanel();
		panel.add(btnGroup);
		this.getElement().getStyle().setZIndex(100000);
	}

	public void addBtn(String cmd) {
		PushButton btn = createBtn(cmd);
		btns.add(btn);
		btnGroup.add(btn);
	}

	private PushButton createBtn(String cmd) {
		Image icon = getIcon(cmd);
		PushButton btn1 = new PushButton(cmd);
		if (icon != null) {
			btn1 = new PushButton(icon);
		}
		btn1.setStylePrimaryName("map_btn");
		btn1.getElement().setAttribute("cmd", cmd);
		btn1.setTitle(cmd);
		btn1.setSize("24px", "24px");
		btn1.addClickHandler(this);
		btn1.getElement().getStyle().setOpacity(0.9);
		btn1.getElement().getStyle().setBorderWidth(0, Unit.PX);
		btn1.getElement().getStyle().setPadding(5, Unit.PX);
		return btn1;
	}

	private Image getIcon(String cmd) {
		ImageResource img = null;
		RS R = RS.INSTANCE;
		if ("select".equals(cmd)) {
			img = R.tools_select();
		} else if ("point".equals(cmd)) {
			img = R.tools_point();
		} else if ("line".equals(cmd)) {
			img = R.tools_line();
		} else if ("polygon".equals(cmd)) {
			img = R.tools_polygon();
		} else if ("zoomIn".equals(cmd)) {
			img = R.tools_zoom_in();
		} else if ("zoomOut".equals(cmd)) {
			img = R.tools_zoom_out();
		} else if ("currentLocation".equals(cmd)) {
			img = R.tools_gps();
		} else if ("switchBaseLayer".equals(cmd)) {
			img = R.tools_switch();
		} else if ("back".equals(cmd)) {
			img = R.tools_prev();
		} else if ("forward".equals(cmd)) {
			img = R.tools_next();
		} else if ("info".equals(cmd)) {
			img = R.tools_info();
		}
		if (img != null) {
			return new Image(img);
		}
		return null;
	}

	public void enable(String cmd) {
		getBtn(cmd).setEnabled(true);
	}

	public void disable(String cmd) {
		getBtn(cmd).setEnabled(false);
	}

	private PushButton getBtn(String cmd) {
		for (PushButton btn : btns) {
			if (cmd.equals(getCmd(btn))) {
				return btn;
			}
		}
		return null;
	}

	public void onClick(ClickEvent event) {
		PushButton btn = (PushButton) event.getSource();
		String cmd = getCmd(btn);
		map.command(cmd);
	}

	private String getCmd(PushButton btn) {
		String cmd = btn.getElement().getAttribute("cmd");
		return cmd;
	}

	public void update(Control control) {
		if (control != null) {
			for (String cmd : control.getCmd()) {
				getBtn(cmd).removeStyleName("activate");
			}
			String active = control.getActive();
			System.out.println("active=" + active);
			if (active != null) {
				getBtn(active).addStyleName("activate");
			}
		}
	}
}
